import java.io.*;
import java.util.*;

class Team implements Comparable<Team> {
    String name;
    int matchesPlayed;
    int wins;
    int draws;
    int losses;
    int goalsFor;
    int goalsAgainst;
    int points;

    public Team(String name) {
        this.name = name;
        this.matchesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    public void update(int homeGoals, int awayGoals, boolean isHome) {
        matchesPlayed++;
        if (isHome) {
            goalsFor += homeGoals;
            goalsAgainst += awayGoals;
            if (homeGoals > awayGoals) {
                wins++;
                points += 3;
            } else if (homeGoals == awayGoals) {
                draws++;
                points += 1;
            } else {
                losses++;
            }
        } else {
            goalsFor += awayGoals;
            goalsAgainst += homeGoals;
            if (awayGoals > homeGoals) {
                wins++;
                points += 3;
            } else if (awayGoals == homeGoals) {
                draws++;
                points += 1;
            } else {
                losses++;
            }
        }
    }

    @Override
    public int compareTo(Team other) {
        if (this.points != other.points) {
            return other.points - this.points;
        }
        if (this.getGoalDifference() != other.getGoalDifference()) {
            return other.getGoalDifference() - this.getGoalDifference();
        }
        return other.goalsFor - this.goalsFor;
    }

    public int getGoalDifference() {
        return goalsFor - goalsAgainst;
    }

    @Override
    public String toString() {
        return String.format("%-15s %2d %2d %2d %2d %3d %3d %2d",
                name, matchesPlayed, wins, draws, losses, goalsFor, goalsAgainst, points);
    }
}

public class FootballLeague {
    public static void main(String[] args) {
        List<Team> teams = readTeamsFromFile("teams.txt");
        if (teams != null && !teams.isEmpty()) {
            displayTeams(teams);
            updateTeamsWithNewMatch(teams);
            Collections.sort(teams);
            displayAndWriteTeamsToFile(teams, "teams.txt");
        } else {
            System.out.println("没有从文件中读取到任何队伍信息。");
        }
    }

    private static List<Team> readTeamsFromFile(String filename) {
        List<Team> teams = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            boolean isFirstLine = true;
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String[] parts = line.trim().split("\\s+");
                if (parts.length != 8) {
                    System.out.println("警告：文件格式不正确，跳过此行：" + line);
                    continue;
                }
                Team team = new Team(parts[0]);
                team.matchesPlayed = Integer.parseInt(parts[1]);
                team.wins = Integer.parseInt(parts[2]);
                team.draws = Integer.parseInt(parts[3]);
                team.losses = Integer.parseInt(parts[4]);
                team.goalsFor = Integer.parseInt(parts[5]);
                team.goalsAgainst = Integer.parseInt(parts[6]);
                team.points = Integer.parseInt(parts[7]);
                teams.add(team);
            }
        } catch (IOException e) {
            System.out.println("读取文件时发生错误: " + filename);
            e.printStackTrace();
        }
        return teams;
    }

    private static void displayTeams(List<Team> teams) {
        Collections.sort(teams);
        System.out.println(String.format("%-15s %2s %2s %2s %2s %3s %3s %2s",
                "队名", "场次", "胜", "平", "负", "进球", "失球", "积分"));
        for (Team team : teams) {
            System.out.println(team);
        }
    }

    private static void updateTeamsWithNewMatch(List<Team> teams) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新的比赛结果：");
        System.out.print("主队名称: ");
        String homeTeamName = scanner.nextLine();
        System.out.print("客队名称: ");
        String awayTeamName = scanner.nextLine();
        System.out.print("主队得分: ");
        int homeScore = scanner.nextInt();
        scanner.nextLine();
        System.out.print("客队得分: ");
        int awayScore = scanner.nextInt();
        scanner.nextLine();

        for (Team team : teams) {
            if (team.name.equals(homeTeamName)) {
                team.update(homeScore, awayScore, true);
            } else if (team.name.equals(awayTeamName)) {
                team.update(homeScore, awayScore, false);
            }
        }
    }

    private static void displayAndWriteTeamsToFile(List<Team> teams, String filename) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
            System.out.println(String.format("%-15s %2s %2s %2s %2s %3s %3s %2s",
                    "队名", "场次", "胜", "平", "负", "进球", "失球", "积分"));
            bw.write(String.format("%-15s %2s %2s %2s %2s %3s %3s %2s\n",
                    "队名", "场次", "胜", "平", "负", "进球", "失球", "积分"));
            for (Team team : teams) {
                System.out.println(team);
                bw.write(team.toString() + "\n");
            }
        } catch (IOException e) {
            System.out.println("写入文件时发生错误: " + filename);
            e.printStackTrace();
        }
    }
}
